﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：自定义对话框窗体
//
//创建标识：米海波 || 2011-04-01
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Hmc.IM.Common.UserControls;
using Hmc.IM.Common.Base;

namespace Hmc.IM.Common.Forms
{
    /// <summary>
    /// 公共弹出窗体
    /// </summary>
    public partial class DialogForm : Form, IBaseForm
    {
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        /// <summary>
        /// 系统API函数，向系统发送消息
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="Msg"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        /// <summary>
        /// 系统API函数，移动窗体
        /// </summary>
        /// <returns></returns>
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        /// <summary>
        /// 标题
        /// </summary>
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                lbTitle.Text = value;
            }
        }

        /// <summary>
        /// 构造弹出窗体
        /// </summary>
        public DialogForm()
        {
            InitializeComponent();
            this.ImeMode = System.Windows.Forms.ImeMode.KatakanaHalf;
            this.TransparencyKey = this.BackColor;
            pTitle.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            lbTitle.MouseDown += new MouseEventHandler(pTitle_MouseDown);
            this.Width = base.Width;
            this.Height = base.Height;
        }

        /// <summary>
        /// 权限ID
        /// </summary>
        private int _powerId = -1;

        /// <summary>
        /// 权限ID
        /// </summary>
        public int PowerId
        {
            get
            {
                return _powerId;
            }
            set
            {
                _powerId = value;
            }
        }

        /// <summary>
        /// 是否已经打开一次
        /// </summary>
        private bool isPower = false;
        /// <summary>
        /// 重写OnVisibleChanged
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (DesignMode) return;
            if (PowerId == -1)
            {
                this.Close();
                //已经打开一次
                if (isPower) return;
                isPower = true;
                Exception ex = new Exception("请对权限ID进行赋值!");
                ex.Source = this.Name;
                throw ex;
            }
            if (!this.IsRight(PowerId) && PowerId != 0)
            {
                //已经打开一次
                if (isPower) return;
                MessageBox.Show("无该模块操作权限,请联系管理员！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                isPower = true;
                this.Close();
            }
        }


        /// <summary>
        /// 根据权限点判断是否拥有权限
        /// </summary>
        /// <param name="rightID"></param>
        /// <returns></returns>
        public virtual bool IsRight(int rightID)
        {
            var query = from p in LoginUserEntity.PermissionsPoint where p == rightID select p;
            if (query.Count() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 鼠标按下移动窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        /// <summary>
        /// 鼠标进入关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = Properties.Resources.close_;
        }

        /// <summary>
        /// 鼠标离开关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.BackgroundImage = Properties.Resources.close;
        }
        /// <summary>
        /// 鼠标点击关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        /// <summary>
        /// 验证指定集合控件下的数据格式
        /// </summary>
        /// <param name="controls">需验证的数据集合</param>
        /// <returns></returns>
        public bool CheckValidate(Control controls)
        {
            bool fay = true;
            foreach (Control ctl in controls.Controls)
            {
                if (ctl is IValidate)
                {
                    IValidate box = ctl as IValidate;
                    if (!box.Validate())
                    {
                        fay = false;
                    }
                }
                else if (ctl.Controls.Count > 0)
                {
                    if (!CheckValidate(ctl))
                        fay = false;
                }
            }
            return fay;
        }

        /// <summary>
        /// 验证当前窗体集合下的数据格式
        /// </summary>
        /// <returns></returns>
        public bool CheckValidate()
        {
            return CheckValidate(this);
        }

        /// <summary>
        /// 调整窗体大小
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            panel3.Size = new System.Drawing.Size(this.Size.Width, panel3.Size.Height);
            panel3.Location = new Point(0, this.Height - panel3.Height);
            base.OnResize(e);
        }
    }
}
